package com.ruoyi.common.convert;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateTimeConverter
 * @Description TODO
 * @Author jiayuan
 * @Date 2023/9/6 9:13
 * @Version 1.0
 */
public class DateTimeConverter implements Converter<Date> {

    private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";

    @Override
    public Class supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Date convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
        return dateFormat.parse(cellData.getStringValue());
    }

    @Override
    public WriteCellData convertToExcelData(Date dateTime, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
        String dateValue = dateFormat.format(dateTime);
        return new WriteCellData<>(dateValue);
    }
}
